package com.cn.wanxi.util;

import java.sql.*;

public class JDBC {
    /**
     * 1.取得数据库链接
     * 2.编译sql语句
     * 3.执行sql语句
     * 4.封装数据
     * 5.返回记过
     */
    private static final String URL = "jdbc:mysql://121.199.55.96:3306/canyin04?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC";
    private static final String USER = "canyin04";
    private static final String PASS = "123456";

    private static Connection getConnection() {

        Connection connection = null;
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//通过反射调用mysql驱动包，加载驱动
            connection = DriverManager.getConnection(URL, USER, PASS);//链接数据库
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static int update(String sql) {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        int resutl = 0;
        try {
//            sql="insert into user (username,sex,phone)values (?,,?,?)";
            statement = connection.prepareStatement(sql);//编译sql
//            statement.setInt(1,1);
////            statement.setString(2,"");
//            statement.setString(2,"");
            resutl = statement.executeUpdate();//执行sql
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resutl;
    }

    public static ResultSet query(String sql) {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement(sql);//编译sql
            resultSet = statement.executeQuery();//执行sql
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }


}
